const path = require('path');
const webpack = require('webpack');
var PurifyCSS = require('purifycss-webpack');
var globAll = require('glob-all');
const ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
console.log(__dirname)
module.exports = {
  entry: {
    app: './src/app.js'
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name]-[hash:5].bundle.js',
    chunkFilename: '[name]-[hash:5].chunk.js'
  },
  resolve: {
    alias: {
      jquery$: path.resolve(__dirname, 'src/libs/jquery.min.js')
    }
  },
  module: {
    rules: [{
      test: /\.less$/,
      use: ExtractTextWebpackPlugin.extract({
        fallback: {
          loader: 'style-loader',
          options: {
            singleton: true,
            // transform: './css.transform.js'
          }
        },
        use: [{
          loader: 'css-loader',
          options: {
            // minimize: true,
            // localIdentName: '[path][name]_[local]_[hash:base64:5]',
            // modules: true
          }
          // loader: 'file-loader'
        }, {
          loader: 'postcss-loader',
          options: {
            ident: 'postcss',
            plugins: [
              require('postcss-sprites')({
                spritePath: 'dist/assets/imgs/sprites',
                retina: true
              }),
              require('postcss-cssnext')()
            ]
          }
        }, {
          loader: 'less-loader'
        }]
      })
    }, {
      test: /\.js$/,
      use: [{
        loader: 'babel-loader',
        options: {
          presets: ['env'],
          plugins: ['lodash']
        }
      }]
    }, {
      test: /\.html$/,
      use: [
        {
          loader: 'html-loader',
          options: {
            attrs: ['img:src', 'img:data-src']
          }
        }
      ]
    },{
      test: /\.(png|jpg|jpeg|gif)$/,
      use: [
        // {
        //   loader: 'file-loader',
        //   options: {
        //     outputPath: 'dist/',
        //     pulicPath: '',
        //     name: '[name]-[hash:5].[ext]',
        //     useRelativePath: true
        //   }
        // }
        {
          loader: 'url-loader',
          options: {
            name: '[name].min.[ext]',
            limit: 100,
            pulicPath: '',
            outputPath: 'assets/imgs/'
          }
        }, {
          loader: 'img-loader',
          options: {
            pngquant: {
              quality: 80
            }
          }
        }
      ]
    }]
  },
  plugins: [
    new ExtractTextWebpackPlugin({
      filename: '[name]-[hash:5].min.css',
      allChunks: false // 给css提取一个范围
    }),
    new webpack.ProvidePlugin({
      $: 'jquery'
    }),
    new PurifyCSS({
      paths: globAll.sync([
        path.join(__dirname, './*.html'),
        path.join(__dirname, './src/*.js')
      ])
    }),
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: './index.html',
      chunks: ['app'],
      minify: {
        collapseWhitespace: true
      }
    }),
    new webpack.optimize.UglifyJsPlugin()
  ]
}